package com.aode.playlist.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.aode.common.bean.response.Mllist;
import com.aode.common.bean.response.Music;
import com.aode.playlist.R;
import com.aode.playlist.databinding.ItemPlaylistBinding;

import java.util.ArrayList;

public class PlayListAdapter  extends RecyclerView.Adapter{
    private Context context;
    private ArrayList<Mllist>  mllistArrayList;
    private MllistInterface mllistInterface;
    public PlayListAdapter(Context context,ArrayList<Mllist>  mllistArrayList,MllistInterface mllistInterface) {
        this.context = context;
        this.mllistArrayList = mllistArrayList;
        this.mllistInterface = mllistInterface;
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemPlaylistBinding playlistBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_playlist,parent,false);
        return new PlayListViewHolder(playlistBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof  PlayListViewHolder){
            PlayListViewHolder playListViewHolder = (PlayListViewHolder) holder;
            if(mllistArrayList!=null&&!mllistArrayList.isEmpty()&&mllistArrayList.size()>0){
                Mllist mllist = mllistArrayList.get(position);
                playListViewHolder.getPlaylistBinding().setMllist(mllist);
                playListViewHolder.getPlaylistBinding().setMllistInterface(mllistInterface);
            }

        }
    }

    @Override
    public int getItemCount() {
        return mllistArrayList.size();
    }

    class PlayListViewHolder extends RecyclerView.ViewHolder{
        private ItemPlaylistBinding playlistBinding;
        public PlayListViewHolder(ItemPlaylistBinding playlistBinding) {
            super(playlistBinding.getRoot());
            this.playlistBinding = playlistBinding;
        }

        public ItemPlaylistBinding getPlaylistBinding() {
            return playlistBinding;
        }

        public void setPlaylistBinding(ItemPlaylistBinding playlistBinding) {
            this.playlistBinding = playlistBinding;
        }
    }

    public interface MllistInterface{
        void onItemClick(Mllist mllist);
    }
}
